package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserChainInfo;
import com.sc.nft.mapper.UserChainInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户链信息表(sc_user_chain_info)数据DAO
 *
 * @author kancy
 * @since 2022-10-20 23:17:03
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class UserChainInfoDao extends ServiceImpl<UserChainInfoMapper, UserChainInfo> {


    public List<UserChainInfo> getNoRegisterUser(){
        LambdaQueryWrapper<UserChainInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserChainInfo::getStatus,Boolean.FALSE);
        wrapper.orderByAsc(UserChainInfo::getId);
        wrapper.last("limit 100");
        return list(wrapper);
    }

    public List<UserChainInfo> getListByStatus(Boolean status,long start,long end){
        LambdaQueryWrapper<UserChainInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserChainInfo::getStatus,status);
        wrapper.orderByAsc(UserChainInfo::getId);
        wrapper.last("limit "+start+" , "+end);
        return list(wrapper);
    }


    public UserChainInfo getRegisterByUserId(Long userId){
        LambdaQueryWrapper<UserChainInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserChainInfo::getStatus,Boolean.TRUE);
        wrapper.eq(UserChainInfo::getUserId,userId);
        return getOne(wrapper);
    }

    public UserChainInfo getByAddr(String userAddr) {
        LambdaQueryWrapper<UserChainInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserChainInfo::getStatus,Boolean.TRUE);
        wrapper.eq(UserChainInfo::getAddress,userAddr);
        return getOne(wrapper);
    }

    public UserChainInfo getByUserId(Long userId) {
        LambdaQueryWrapper<UserChainInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserChainInfo::getUserId,userId);
        return getOne(wrapper);
    }

    public UserChainInfo getByAddress(String userAddr) {
        LambdaQueryWrapper<UserChainInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserChainInfo::getAddress,userAddr);
        return getOne(wrapper);
    }

    public Boolean updatePublicKeyAndAddressAndStatusById(String publicKey, String address, String privateKey, Boolean status,Long id) {
        LambdaUpdateWrapper<UserChainInfo> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(UserChainInfo::getId,id);
        wrapper.set(UserChainInfo::getPublicKey,publicKey);
        wrapper.set(UserChainInfo::getAddress,address);
        wrapper.set(UserChainInfo::getUserKey,privateKey);
        wrapper.set(UserChainInfo::getStatus,status);
        return update(wrapper);
    }

    public List<UserChainInfo> getAllListByStatus() {
        LambdaQueryWrapper<UserChainInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserChainInfo::getStatus,Boolean.TRUE);
        return list(wrapper);
    }
}